//
//  TTGemstonesView.m
//  TaoTuo
//
//  Created by admin on 2019/8/21.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTGemstonesView.h"
#import "TTGemstonesRightView.h"
#import "TTSearchStoneModel.h"
#import "TTFiltrateView.h"
#import "TTCommondDataModel.h"

@interface TTGemstonesView ()<UITextFieldDelegate>

@property (nonatomic, strong) TTGemstonesRightView *rightView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *showBGView;
@property (nonatomic, strong) TTFiltrateView *showFilteView;

@property (nonatomic, strong) NSArray *filtesArray;

@property (nonatomic, strong) UIButton *zlBtn;  // 重量
@property (nonatomic, strong) UIButton *zsBtn;  // 证书

/// 是否包含异行
@property (nonatomic, assign) BOOL isHasDifferent;
/// 记录参数是否发生变化了
@property (nonatomic, copy) NSString *flag;


@end

@implementation TTGemstonesView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.resultDic = [NSMutableDictionary dictionary];
        
        [self addObserver:self forKeyPath:@"flag" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        
    }
    return self;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.showBGView.hidden = NO;
    [self showFilteView];
    return YES;
}


- (void)resetView
{
    [self.resultDic removeAllObjects];
    
    for (TTFiltrateView *fV in self.filtesArray) {
        [fV resetView];
    }
    
    [self.rightView resetView];
    [self.rightView setSelectedFirstItem];
    self.zlMinTF.text = @"";
    self.zlMaxTF.text = @"";
    self.zsTF.text = @"";
    
    
}

// 右侧视图搜索数据
- (NSString *)setupRightViewResultDataWithStartValue:(NSString *)startValue key:(NSString *)key value:(NSString *)value
{
    startValue = [startValue stringByAppendingString:[NSString stringWithFormat:@"%@,", value]];
    
    self.resultDic[key] = startValue;
    return startValue;
}

// 点击重量
- (void)zlBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    self.zsBtn.selected = NO;
    self.zsTF.hidden = YES;
    [self endEditing:YES];
}

// 点击证书
- (void)zsBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    self.zlBtn.selected = NO;
    self.zsTF.hidden = NO;
    [self endEditing:YES];
    self.showBGView.hidden = YES;
}

- (void)parseData
{
    // 裸石搜索的数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"search_stone" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    // 右侧视图数据
    NSMutableArray *rightDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"rightScope"]) {
        [rightDataArray addObject:[TTSearchStoneModel modelWithJSON:mdic]];
    }
    
    // 切工/抛光/对称
    __block NSMutableArray *leftOneDataArray = [NSMutableArray array];
    NSMutableArray *leftOne2DataArray = [NSMutableArray array];
    NSMutableArray *leftOne3DataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"defBean2"]) {
        [leftOne2DataArray addObject:mdic[@"title"]];
    }
    for (NSDictionary *mdic in dic[@"defBean3"]) {
        [leftOne3DataArray addObject:mdic[@"title"]];
    }
    // 默认是三个，当选中的石头包含异形，就要用2个的了
    leftOneDataArray = leftOne3DataArray;
    NSMutableArray *leftTwoDataArrayDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"symmetricBean"]) {
        [leftTwoDataArrayDataArray addObject:mdic[@"title"]];
    }
    NSMutableArray *leftThreeDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"polishingBean"]) {
        [leftThreeDataArray addObject:mdic[@"title"]];
    }
    NSMutableArray *leftFourDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"cutBean"]) {
        [leftFourDataArray addObject:mdic[@"title"]];
    }
    // 荧光
    NSMutableArray *leftFiveDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"fluorescenceBean"]) {
        [leftFiveDataArray addObject:mdic[@"title"]];
    }
    // 证书
    NSMutableArray *leftSixDataArray = [NSMutableArray array];
    for (NSDictionary *mdic in dic[@"certBean"]) {
        [leftSixDataArray addObject:mdic[@"title"]];
    }
    
    
    // 右侧视图
    @weakify(self)
    _rightView = [[TTGemstonesRightView alloc] initWithFrame:CGRectMake(0, 5, 45 * 3 + 2, self.height - 5) dataArray:rightDataArray selectArrayBlock:^(NSArray * _Nullable oneSelectedArray, NSArray * _Nullable twoSelectedArray, NSArray * _Nullable threeSelectedArray) {
        
        @strongify(self)
        NSString *shapeStr = @"", *colorStr = @"", *clarityStr = @"";
        for (TTSearchStoneModel *model in oneSelectedArray) {
            shapeStr = [shapeStr stringByAppendingString:[NSString stringWithFormat:@"%@,", model.shape]];
        }
        if (shapeStr.length > 0) {
            shapeStr = [shapeStr substringToIndex:shapeStr.length - 1];
            self.resultDic[@"param.shape"] = shapeStr;
        }
        
        if (oneSelectedArray.count > 1) {
            self.isHasDifferent = YES;
        } else if (oneSelectedArray.count == 1) {
            TTSearchStoneModel *model = oneSelectedArray.firstObject;
            self.isHasDifferent = ![model.shape isEqualToString:@"ROUND"];
        } else {
            self.isHasDifferent = NO;
        }
        
        for (TTSearchStoneModel *model in twoSelectedArray) {
            colorStr = [colorStr stringByAppendingString:[NSString stringWithFormat:@"%@,", model.title]];
        }
        if (colorStr.length > 0) {
            colorStr = [colorStr substringToIndex:colorStr.length - 1];
            self.resultDic[@"param.color"] = colorStr;
        }

        for (TTSearchStoneModel *model in threeSelectedArray) {
            clarityStr = [clarityStr stringByAppendingString:[NSString stringWithFormat:@"%@,", model.title]];
        }
        if (clarityStr.length > 0) {
            clarityStr = [clarityStr substringToIndex:clarityStr.length - 1];
            self.resultDic[@"param.clarity"] = clarityStr;
        }
        
        if (oneSelectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.shape"];
        }
        if (twoSelectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.color"];
        }
        if (threeSelectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.clarity"];
        }
        
        [self chooseParamsChanged];
        
    }];
    [self addSubview:_rightView];
    // 设置默认选中的
    if (self.diamodParamsModel == nil) {
        [_rightView setSelectedFirstItem];
    } else {
        NSDictionary *shapeDic = [TTCommondDataModel findShapeImageName];
        NSInteger shapeIndex = 0, colorIndex = 0, clarityIndex = 0;
        for (int i = 0; i < rightDataArray.count; i++) {
            TTSearchStoneModel *m = rightDataArray[i];
            if (i % 3 == 0 && [shapeDic[self.diamodParamsModel.Shape] isEqualToString:m.imageName]) {
                shapeIndex = i;
                continue;
            }
            if (i % 3 == 1 && [self.diamodParamsModel.Color isEqualToString:m.title]) {
                colorIndex = i;
                continue;
            }
            if (i %3 == 2 && [self.diamodParamsModel.Clarity isEqualToString:m.title]) {
                clarityIndex = i;
                continue;
            }
        }
        [_rightView setSelectedWithIndexs:@[@(shapeIndex), @(colorIndex), @(clarityIndex)]];
    }
    
    // 左侧底视图
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.rightView.right + 5, 0, self.width - self.rightView.width - 10, self.height)];
    [self addSubview:_scrollView];
    
    CGFloat lW = (_scrollView.width - 10) * 0.5;
    UIButton *zlBtn = [UIButton createButtonWithTitle:@"重量ct" normalTitleColor:UIColorHex(666666) selectedTitleColor:[UIColor blackColor] titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(zlBtnClick:)];
    zlBtn.frame = CGRectMake(0, 0, lW, 30);
    zlBtn.selected = YES;
    [_scrollView addSubview:zlBtn];
    self.zlBtn = zlBtn;
    
    UIButton *zsBtn = [UIButton createButtonWithTitle:@"证书号" normalTitleColor:UIColorHex(666666) selectedTitleColor:[UIColor blackColor] titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(zsBtnClick:)];
    zsBtn.frame = CGRectMake(zlBtn.right, zlBtn.top, zlBtn.width, 30);
    [_scrollView addSubview:zsBtn];
    self.zsBtn = zsBtn;
    
    [LLLineUtilies addLineInView:_scrollView startPoint:CGPointMake(zlBtn.right, 8) endPoint:CGPointMake(zlBtn.right, 22) lineColor:UIColorHex(333333)];
    
    CGFloat tfW = (_scrollView.width - 25) * 0.5;
    _zlMinTF = [[UITextField alloc] initWithFrame:CGRectMake(0, zlBtn.bottom, tfW, 30)];
    _zlMinTF.font = [UIFont systemFontOfSize:14];
    _zlMinTF.keyboardType = UIKeyboardTypeDecimalPad;
    _zlMinTF.delegate = self;
    _zlMinTF.textAlignment = NSTextAlignmentCenter;
    _zlMinTF.textColor = UIColorHex(333333);
//    _zlMinTF.layer.borderColor = UIColorHex(C1C2C2).CGColor;
//    _zlMinTF.layer.borderWidth = 1;
    _zlMinTF.backgroundColor = UIColorHex(F0F4F7);
    [_scrollView addSubview:_zlMinTF];
    _zlMinTF.text = self.diamodParamsModel.DiaSize;
    
    [LLLineUtilies addLineInView:_scrollView startPoint:CGPointMake(_zlMinTF.right + 5, _zlMinTF.centerY) endPoint:CGPointMake(_zlMinTF.right + 10, _zlMinTF.centerY) lineColor:UIColorHex(333333)];
    
    _zlMaxTF = [[UITextField alloc] initWithFrame:CGRectMake(_zlMinTF.right + 15, _zlMinTF.top, tfW, _zlMinTF.height)];
    _zlMaxTF.delegate = self;
    _zlMaxTF.font = [UIFont systemFontOfSize:14];
    _zlMaxTF.keyboardType = UIKeyboardTypeDecimalPad;
    _zlMaxTF.textAlignment = NSTextAlignmentCenter;
    _zlMaxTF.textColor = UIColorHex(333333);
//    _zlMaxTF.layer.borderColor = UIColorHex(C1C2C2).CGColor;
//    _zlMaxTF.layer.borderWidth = 1;
    _zlMaxTF.backgroundColor = UIColorHex(F0F4F7);
    [_scrollView addSubview:_zlMaxTF];
    
    [[self.zlMinTF rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        NSString *text = x;
        NSString *afterDotCount = @"0"; // 小数点后的第一位数字
        if ([x containsString:@"."]) {
            // 是小数，取小数点后的第一位数字
            NSArray *textArray = [x componentsSeparatedByString:@"."];
            text = textArray.firstObject;
            if ([textArray.lastObject length] > 0) {
                afterDotCount = [textArray.lastObject substringToIndex:1];
            }
        }
        if (x.length > 0) {
            self.zlMaxTF.text = [NSString stringWithFormat:@"%@.%@9", text, afterDotCount];
        } else {
            self.zlMaxTF.text = @"";
        }
        
        [self chooseParamsChanged];
        
    }];
    
    _zsTF = [[UITextField alloc] initWithFrame:CGRectMake(_zlMinTF.left, _zlMinTF.top, self.scrollView.width - 10, _zlMinTF.height)];
    _zsTF.hidden = YES;
    _zsTF.font = [UIFont systemFontOfSize:14];
    _zsTF.keyboardType = UIKeyboardTypeNumberPad;
    _zsTF.textAlignment = NSTextAlignmentCenter;
    _zsTF.textColor = UIColorHex(333333);
//    _zsTF.layer.borderColor = UIColorHex(C1C2C2).CGColor;
//    _zsTF.layer.borderWidth = 1;
    _zsTF.backgroundColor = UIColorHex(F0F4F7);
    [_scrollView addSubview:_zsTF];
    
    [_zsTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        [self chooseParamsChanged];
    }];
    
    
    __block TTFiltrateView *_twoFilteView, *_threeFilteView, *_fourFilteView;
    CGFloat viewH = 28 * kScale;
    UILabel *qgLabel = [UILabel labelWithTitle:@"切工/抛光/对称" color:UIColorHex(333333) fontSize:14];
    qgLabel.frame = CGRectMake(0, _zlMinTF.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:qgLabel];
    
    TTFiltrateView *oneFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, qgLabel.bottom, _scrollView.width, viewH) allowsMultiple:NO selectedItemArrayAndViewBlock:^(TTFiltrateView * _Nullable filtrateView, NSArray * _Nullable selectedArray, NSInteger index) {
        
        if (index == leftOneDataArray.count - 1) {
            [filtrateView resetView];
            [_twoFilteView resetView];
            [_threeFilteView resetView];
            [_fourFilteView resetView];
        } else {
            [_twoFilteView selectWithIndex:index];
            [_threeFilteView selectWithIndex:index];
            [_fourFilteView selectWithIndex:index];
        }
        
        [self chooseParamsChanged];
        
    }];
    
    [oneFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [oneFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    oneFilteView.canCancelSelect = NO;
    oneFilteView.dataArray = leftOneDataArray;
    [_scrollView addSubview:oneFilteView];
    
    TTFiltrateView *twoFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, oneFilteView.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0 || self.isHasDifferent) {
            [self.resultDic removeObjectForKey:@"param.cut"];
            return;
        }
        NSString *curStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.cut"] = curStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [twoFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [twoFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    twoFilteView.dataArray = leftTwoDataArrayDataArray;
    [_scrollView addSubview:twoFilteView];
    _twoFilteView = twoFilteView;
    if (self.diamodParamsModel.Cut.length > 0) {
        [twoFilteView setSelectedItemWithIndex:[leftTwoDataArrayDataArray indexOfObject:self.diamodParamsModel.Cut]];
    }
    
    TTFiltrateView *threeFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, twoFilteView.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.polish"];
            return;
        }
        
        NSString *polishStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.polish"] = polishStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [threeFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [threeFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    threeFilteView.dataArray = leftThreeDataArray;
    [_scrollView addSubview:threeFilteView];
    _threeFilteView = threeFilteView;
    if (self.diamodParamsModel.Polish.length > 0) {
        [_threeFilteView setSelectedItemWithIndex:[leftThreeDataArray indexOfObject:self.diamodParamsModel.Polish]];
    }
    
    TTFiltrateView *fourFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, threeFilteView.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.sym"];
            
            [self chooseParamsChanged];
            
            return;
        }
        
        NSString *symStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.sym"] = symStr;
        
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [fourFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [fourFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    fourFilteView.dataArray = leftFourDataArray;
    [_scrollView addSubview:fourFilteView];
    _fourFilteView = fourFilteView;
    if (self.diamodParamsModel.Sym.length > 0) {
        [_fourFilteView setSelectedItemWithIndex:[leftFourDataArray indexOfObject:self.diamodParamsModel.Sym]];
    }
    UILabel *hintLabel = [UILabel labelWithTitle:@"您选择的形状包含异形钻，不能筛选切工！" color:[UIColor redColor] fontSize:10];
    hintLabel.frame = fourFilteView.frame;
    hintLabel.hidden = YES;
    [_scrollView addSubview:hintLabel];
    // 根据选择情况设置数据源
    [[RACObserve(self, isHasDifferent) distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        
        if ([x boolValue]) {
            leftOneDataArray = leftOne2DataArray;
            threeFilteView.top = oneFilteView.bottom;
            fourFilteView.top = threeFilteView.bottom;
            twoFilteView.hidden = YES;
            hintLabel.hidden = NO;
//            [[UIApplication sharedApplication].keyWindow makeToast:@"您选择的形状包含异形钻，不能筛选切工！" duration:2 position:CSToastPositionCenter];
            
//            [UIView animateWithDuration:0.3 animations:^{
//                threeFilteView.top = oneFilteView.bottom;
//                fourFilteView.top = threeFilteView.bottom;
//                twoFilteView.alpha = 0;
//            } completion:^(BOOL finished) {
//                twoFilteView.hidden = YES;
//            }];
//            qgLabel.text = @"抛光/对称";
            
            
        } else {
            leftOneDataArray = leftOne3DataArray;
            threeFilteView.top = twoFilteView.bottom;
            fourFilteView.top = threeFilteView.bottom;
            twoFilteView.hidden = NO;
            hintLabel.hidden = YES;
//            [UIView animateWithDuration:0.3 animations:^{
//                threeFilteView.top = twoFilteView.bottom;
//                fourFilteView.top = threeFilteView.bottom;
//                twoFilteView.alpha = 1;
//            }];
//
//            qgLabel.text = @"切工/抛光/对称";
        }
        
        [twoFilteView resetView];
        [threeFilteView resetView];
        [fourFilteView resetView];
        oneFilteView.dataArray = leftOneDataArray;
        
        [self chooseParamsChanged];
        
    }];
    
    
    
    UILabel *ygLabel = [UILabel labelWithTitle:@"荧光" color:UIColorHex(333333) fontSize:14];
    ygLabel.frame = CGRectMake(0, fourFilteView.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:ygLabel];
    
    TTFiltrateView *ygFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, ygLabel.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.flour"];
            
            [self chooseParamsChanged];
            
            return;
        }
        
        NSString *flourStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.flour"] = flourStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [ygFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [ygFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    ygFilteView.dataArray = leftFiveDataArray;
    [_scrollView addSubview:ygFilteView];
    if (self.diamodParamsModel.Flour.length > 0) {
        [ygFilteView setSelectedItemWithIndex:[leftFiveDataArray indexOfObject:self.diamodParamsModel.Flour]];
    }
    
    UILabel *zsfLabel = [UILabel labelWithTitle:@"证书" color:UIColorHex(333333) fontSize:14];
    zsfLabel.frame = CGRectMake(0, ygFilteView.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:zsfLabel];
    
    TTFiltrateView *zsFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, zsfLabel.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.cert"];
            
            [self chooseParamsChanged];
            
            return;
        }
        
        NSString *certStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.cert"] = certStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [zsFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [zsFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    zsFilteView.dataArray = leftSixDataArray;
    [_scrollView addSubview:zsFilteView];
    if (self.diamodParamsModel.Cert.length > 0) {
        [zsFilteView setSelectedItemWithIndex:[leftSixDataArray indexOfObject:self.diamodParamsModel.Cert]];
    }
    
    UILabel *locationLabel = [UILabel labelWithTitle:@"所在地" color:UIColorHex(333333) fontSize:14];
    locationLabel.frame = CGRectMake(0, zsFilteView.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:locationLabel];
    
    TTFiltrateView *locationFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, locationLabel.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.location"];
            
            [self chooseParamsChanged];
            
            return;
        }
        
        NSString *locationStr = [selectedArray componentsJoinedByString:@","];
        self.resultDic[@"param.location"] = locationStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [locationFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [locationFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    NSArray *locationArray = @[@"国内", @"香港", @"印度", @"欧美"];
    locationFilteView.dataArray = locationArray;
    [_scrollView addSubview:locationFilteView];
    if (self.diamodParamsModel.Location.length > 0) {
        [locationFilteView setSelectedItemWithIndex:[locationArray indexOfObject:self.diamodParamsModel.Location]];
    }
    
    
    NSMutableAttributedString *hintAttr = [[NSMutableAttributedString alloc] initWithString:@"产地 *南非钻数量稀少，不建议太多条件一起查询"];
    [hintAttr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8], NSForegroundColorAttributeName : UIColorHex(#FE7575)} range:[hintAttr.string rangeOfString:@"*南非钻数量稀少，不建议太多条件一起查询"]];
    NSMutableAttributedString *notHintAttr = [[NSMutableAttributedString alloc] initWithString:@"产地"];
    UILabel *originLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:14];
    originLabel.attributedText = notHintAttr;
    originLabel.frame = CGRectMake(0, locationFilteView.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:originLabel];
    
    TTFiltrateView *originFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, originLabel.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.isFromSouthAfrica"];
            originLabel.attributedText = notHintAttr;
            
            [self chooseParamsChanged];
            
            return;
        }
        originLabel.attributedText = hintAttr;
        self.resultDic[@"param.isFromSouthAfrica"] = @"true";
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [originFilteView setFixWidthAndHeight:CGSizeMake(60, viewH - 1)];
    [originFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    [originFilteView setFixWidthAndHeight:CGSizeMake((oneFilteView.width - 2) * 0.5, viewH - 1)];
    originFilteView.dataArray = @[@"South Africa"];
    [_scrollView addSubview:originFilteView];
    if (self.diamodParamsModel.SouthAfrica) {
        [originFilteView setSelectedFirstItem];
    }
    // 加个推荐的标记
    UIImage *recommendedImage = [UIImage imageNamed:@"tuijianchandi"];
    UIImageView *recommendedImageView = [[UIImageView alloc] initWithImage:recommendedImage];
    recommendedImageView.frame = CGRectMake(0, 2, recommendedImage.size.width, recommendedImage.size.height);
    [originFilteView addSubview:recommendedImageView];
    
    
    UILabel *moreLabel = [UILabel labelWithTitle:@"更多" color:UIColorHex(333333) fontSize:14];
    moreLabel.frame = CGRectMake(0, originFilteView.bottom, _scrollView.width, viewH);
    [_scrollView addSubview:moreLabel];
    
    TTFiltrateView *moreFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, moreLabel.bottom, oneFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if ([selectedArray containsObject:@"肉眼净"]) {
            self.resultDic[@"param.eyeClean"] = @(1);
        } else {
            [self.resultDic removeObjectForKey:@"param.eyeClean"];
        }
        if ([selectedArray containsObject:@"无奶咖"]) {
            self.resultDic[@"param.milky"] = @"N";
            self.resultDic[@"param.browness"] = @"N";
        } else {
            [self.resultDic removeObjectForKey:@"param.milky"];
            [self.resultDic removeObjectForKey:@"param.browness"];
        }
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [moreFilteView setFixWidthAndHeight:CGSizeMake((oneFilteView.width - 2) * 0.5, viewH - 1)];
    [moreFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    moreFilteView.dataArray = @[@"肉眼净", @"无奶咖"];
    [_scrollView addSubview:moreFilteView];
    
    if ([[self.diamodParamsModel.EyeClean lowercaseString] hasPrefix:@"ec"]) {
        [moreFilteView setSelectedFirstItem];
    }
    
    if ([self.diamodParamsModel.Milky isEqualToString:@"无奶"] && [self.diamodParamsModel.Browness isEqualToString:@"无咖"]) {
        [moreFilteView setSelectedItemWithIndex:1];
    }
    
    // 现货，订货
    TTFiltrateView *shopFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, moreFilteView.bottom, moreFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count == 0) {
            [self.resultDic removeObjectForKey:@"param.sysStatus"];
            return;
        }
        NSString *xianhuoStr = @"";
        NSString *dinghuoStr = @"";
        if ([selectedArray containsObject:@"现货"]) {
            xianhuoStr = @"1";
        }
        if ([selectedArray containsObject:@"订货"]) {
            dinghuoStr = @"0";
        }
        NSString *shopStr;
        if (xianhuoStr.length == 0) {
            shopStr = dinghuoStr;
            
        } else {
            if (dinghuoStr.length > 0) {
                shopStr = [NSString stringWithFormat:@"%@,%@", xianhuoStr, dinghuoStr];
            } else {
                shopStr = xianhuoStr;
            }
        }
        
        
        self.resultDic[@"param.sysStatus"] = shopStr;
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [shopFilteView setFixWidthAndHeight:CGSizeMake((oneFilteView.width - 2) * 0.5, viewH - 1)];
    [shopFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    shopFilteView.dataArray = @[@"现货", @"订货"];
    [_scrollView addSubview:shopFilteView];
    
    TTFiltrateView *mediaFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, shopFilteView.bottom, moreFilteView.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if ([selectedArray containsObject:@"有图"]) {
            self.resultDic[@"param.havePictrue"] = @"true";
        } else {
            [self.resultDic removeObjectForKey:@"param.havePictrue"];
        }
        if ([selectedArray containsObject:@"有视频"]) {
            self.resultDic[@"param.haveVidio"] = @"true";
        } else {
            [self.resultDic removeObjectForKey:@"param.haveVidio"];
        }
        
        [self chooseParamsChanged];
        
    } leftRightSpace:0];
    [mediaFilteView setFixWidthAndHeight:CGSizeMake((oneFilteView.width - 2) * 0.5, viewH - 1)];
    [mediaFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
    mediaFilteView.dataArray = @[@"有图", @"有视频"];
    [_scrollView addSubview:mediaFilteView];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.width, 19 * viewH + 30);
    
    
    self.filtesArray = @[oneFilteView, twoFilteView, threeFilteView, fourFilteView, ygFilteView, zsFilteView, locationFilteView, originFilteView, moreFilteView, shopFilteView, mediaFilteView, self.showFilteView];
    
}

/// 参数变化，发送通知
- (void)chooseParamsChanged
{
    self.flag = @"1";
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSString *newF = change[NSKeyValueChangeNewKey];
    NSString *oldF = change[NSKeyValueChangeOldKey];
    NSLog(@">> f = %@", self.flag);
    if ([newF isEqualToString:oldF]) {
        return;
    }
    if (newF.intValue != 1) {
        return;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"luoshi_chooseItemsChangedNotify_key" object:self.resultDic];
        self.flag = @"0";
    });
    
    
}

- (UIView *)showBGView
{
    if (!_showBGView) {
        _showBGView = [[UIView alloc] initWithFrame:CGRectMake(self.scrollView.left, self.zlMinTF.bottom, self.scrollView.width, self.height - self.zlMinTF.bottom)];
        _showBGView.hidden = YES;
        _showBGView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self addSubview:_showBGView];
        
        UIView *kbView = [[UIView alloc] initWithFrame:CGRectMake(0, self.showFilteView.bottom + 5, _showBGView.width, _showBGView.height - self.showFilteView.bottom)];
        [_showBGView addSubview:kbView];
        @weakify(self)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(UITapGestureRecognizer *  _Nonnull sender) {
            @strongify(self)
            UIView *view = sender.view;
            [view.superview.superview endEditing:YES];
            view.superview.hidden = YES;
        }];
        [kbView addGestureRecognizer:tap];
    }
    return _showBGView;
}

- (TTFiltrateView *)showFilteView
{
    if (!_showFilteView) {
        @weakify(self)
        _showFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, 0, self.showBGView.width, 122) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
            @strongify(self)
            NSLog(@"--> %@", selectedArray.firstObject);
            self.showBGView.hidden = YES;
            [self endEditing:YES];
            NSArray *textArray = [selectedArray.firstObject componentsSeparatedByString:@"-"];
            self.zlMinTF.text = textArray.firstObject;
            self.zlMaxTF.text = textArray.lastObject;
        } leftRightSpace:0];
        CGFloat w = ceil((self.showBGView.width - 3) / 3.0);
        [_showFilteView setFixWidthAndHeight:CGSizeMake(w, 29)];
        [_showFilteView setLayoutConfigWithMinimumLineSpacing:1 minimumInteritemSpacing:1 sectionInset:UIEdgeInsetsMake(0, 0, 0, 1)];
        _showFilteView.dataArray = @[
            @"0.30-0.32", @"0.60-0.62", @"0.90-0.92", @"2.00-2.09",
            @"0.40-0.42", @"0.70-0.72", @"1.00-1.03", @"3.00-3.09",
            @"0.50-0.52", @"0.80-0.82", @"1.50-1.59", @"4.00-4.09"
        ];
        
        [self.showBGView addSubview:_showFilteView];
    }
    return _showFilteView;
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView
{
    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"flag"];
}

@end
